﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SecsEquip;
using SecsEquip300;

namespace EquipSecs300
{
    public partial class LoadPortView : CMyFormEx
    {
        public GEM_LoadPort GEMLoadPort { get; set; }

        int m_nPortCount = SecsDataHelper.Function.PortQty;  // 初始化的时候设定好
        Dictionary<string, LoadPortSubView> m_mapView = new Dictionary<string, LoadPortSubView>();
        CMyFormEx m_curActiveView = null;       // 当前活跃窗口

        public LoadPortView(SecsEquipHelper secsEquipHelper)
        {
            m_pViewName = "Load Port";
            this.secsEquipHelper = secsEquipHelper;
            GEMLoadPort = secsEquipHelper.LoadPort;
            InitializeComponent();
            InitCtrl();
        }

        // 更新数据
        public override void UpdateData()
        {

        }
        void InitCtrl()
        {
            bool bShow = true;

            // 添加窗口Load Port
            for (int i = 1; i <= m_nPortCount; i++)
            {
                string pName = "Port_" + i.ToString();

                // port 选项
                comboBoxPort.Items.Add(pName);

                // 新窗口
                LoadPortSubView pView = new LoadPortSubView();
                pView.SetPortNo(i);
                pView.GEMLoadPortSub = GEMLoadPort.MapPort[pName];
                GEMLoadPort.CarrierInfoChangeHandler += pView.OnCarrierInfoChange;
                AddToList(pName, pView);

                // 显示窗口
                if (bShow)
                {
                    comboBoxPort.SelectedIndex = 0;
                    ShowView(pName);
                    bShow = false;
                }
            }

        }

        // 添加数据
        void AddToList(string name, LoadPortSubView view)
        {
            InitView(view);

            m_mapView.Add(name, view);
        }

        // 初始化窗口
        private void InitView(LoadPortSubView view)
        {
            view.TopLevel = false;
            view.BackColor = Color.White;
            view.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
            view.FormBorderStyle = FormBorderStyle.None;

            view.m_pMainView = this;
            view.m_pSecs = secsEquipHelper.m_pSecs;
            view.secsEquipHelper = secsEquipHelper;
            view.UpdateData();
            view.Show();
            view.Hide();

        }

        // 添加显示界面
        void AddPaneView(CMyFormEx view)
        {
            if (view == null)
            {
                return;
            }

            if (m_curActiveView != null)
            {
                m_curActiveView.Hide();
            }
            view.Show();
            panelShow.Controls.Add(view);
            m_curActiveView = view;
        }

        // 显示窗口,设为活跃窗口
        void ShowView(string viewName)
        {
            // 获取对象                   
            CMyFormEx view = GetViewForMap(viewName);
            AddPaneView(view);
        }

        // 寻找相应的map数据
        CMyFormEx GetViewForMap(string name)
        {
            CMyFormEx view = null;
            try
            {
                view = m_mapView[name];
            }
            catch (System.Exception)
            {
                view = null;
            }
            return view;
        }


        private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            string pPort = comboBoxPort.SelectedItem.ToString();
            ShowView(pPort);
        }

    }
}
